package red.service.impl;


import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import red.mapper.Pagemapper;
import red.model.*;
import red.service.PagingService2;
import red.util.Model;
import red.util.PageHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class PagingServiceImpl2 implements PagingService2 {

    @Autowired
    private Pagemapper pagemapper;
    private List<UserModel> data;
    private List<User> list = new ArrayList<>();
    private List<ColumnModel> data1;
    private List<Column> list1 = new ArrayList<>();
    private List<ArticleModel> data2;
    private List<Text> list2= new ArrayList<>();
    private JSONObject object;
    private int name;
    private int total;


    @Override
    public List pagingUser(Model model) {
        String pageSize = (String) model.get("pageSize");
        String page = (String) model.get("page");
        name = 1;
        total = pagemapper.getTotal(name);
        if (page.length() == 0 && pageSize.length() == 0) {
            data = pagemapper.getUserList();
            for (int i = 0; i < data.size(); i++) {
                User user = new User();
                String userName = data.get(i).getUser_name();
                int a = pagemapper.getUserposts(userName);
                user.setUser_name(data.get(i).getUser_name());
                user.setLogintime(data.get(i).getLogintime());
                user.setNumber(a);
                list.add(user);
            }
            PageHelper paging = new PageHelper(total, page, pageSize, list);
            object = new JSONObject();
            try {
                object.put("Total", paging.getTotal());
                object.put("Page", paging.getPage());
                object.put("Pages", paging.getPages());
                object.put("hasPreviousPage", paging.hasPreviousPage());
                object.put("hasNextPage", paging.hasNextPage());
                object.put("list", paging.getList());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            if (page.length() != 0 && pageSize.length() != 0) {
                int count = Integer.parseInt(pageSize);
                int one = Integer.parseInt(page);
                int start = (one-1)*count;
                data = pagemapper.getPageUser(count,start);
                for (int i = 0; i < data.size(); i++) {
                    User user = new User();
                    String userName = data.get(i).getUser_name();
                    int a = pagemapper.getUserposts(userName);
                    user.setUser_name(data.get(i).getUser_name());
                    user.setLogintime(data.get(i).getLogintime());
                    user.setNumber(a);
                    list.add(user);
                }
                PageHelper paging = new PageHelper(total, page, pageSize, list);
                object = new JSONObject();
                try {
                    object.put("Total", paging.getTotal());
                    object.put("Page", paging.getPage());
                    object.put("Pages", paging.getPages());
                    object.put("hasPreviousPage", paging.hasPreviousPage());
                    object.put("hasNextPage", paging.hasNextPage());
                    object.put("list", paging.getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return Collections.singletonList(object);
    }

    @Override
    public List pagingColumn(Model model) {
        String pageSize = (String) model.get("pageSize");
        String page = (String) model.get("page");
        name = 2;
        total = pagemapper.getTotal(name);
        if (page.length() == 0 && pageSize.length() == 0) {
            data1 = pagemapper.getColumnList();
            for (int i = 0; i < data1.size(); i++) {
                Column column = new Column();
                String Name = data1.get(i).getColumn_name();
                int a = pagemapper.getColumnposts(Name);
                column.setColumn_name(data1.get(i).getColumn_name());
                column.setColumn_date(data1.get(i).getColumn_date());
                column.setNumber(a);
                list1.add(column);
            }
            PageHelper paging = new PageHelper(total, page, pageSize, list1);
            object = new JSONObject();
            try {
                object.put("Total", paging.getTotal());
                object.put("Page", paging.getPage());
                object.put("Pages", paging.getPages());
                object.put("hasPreviousPage", paging.hasPreviousPage());
                object.put("hasNextPage", paging.hasNextPage());
                object.put("list", paging.getList());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            if (page.length() != 0 && pageSize.length() != 0) {
                int count = Integer.parseInt(pageSize);
                int one = Integer.parseInt(page);
                int start = (one-1)*count;
                data1 = pagemapper.getPageColumn(count,start);
                for (int i = 0; i < data1.size(); i++) {
                    Column column = new Column();
                    String Name = data1.get(i).getColumn_name();
                    int a = pagemapper.getColumnposts(Name);
                    column.setColumn_name(data1.get(i).getColumn_name());
                    column.setColumn_date(data1.get(i).getColumn_date());
                    column.setNumber(a);
                    list1.add(column);
                }
                PageHelper paging = new PageHelper(total, page, pageSize, list1);
                object = new JSONObject();
                try {
                    object.put("Total", paging.getTotal());
                    object.put("Page", paging.getPage());
                    object.put("Pages", paging.getPages());
                    object.put("hasPreviousPage", paging.hasPreviousPage());
                    object.put("hasNextPage", paging.hasNextPage());
                    object.put("list", paging.getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return Collections.singletonList(object);
    }

    @Override
    public List pagingarticle(Model model) {
        String pageSize = (String) model.get("pageSize");
        String page = (String) model.get("page");
        name = 3;
        total = pagemapper.getTotal(name);
        if (page.length() == 0 && pageSize.length() == 0) {
            data2 = pagemapper.getArticList();
            for (int i = 0; i < data2.size(); i++) {
                Text text = new Text();
                text.setAudit(data2.get(i).getAudit());
                text.setHeadline(data2.get(i).getHeadline());
                text.setPublish_date(data2.get(i).getPublish_date());
                text.setLike_number(data2.get(i).getLike_number());
                text.setPage(data2.get(i).getPage());
                list2.add(text);
            }
            PageHelper paging = new PageHelper(total, page, pageSize, list2);
            object = new JSONObject();
            try {
                object.put("Total", paging.getTotal());
                object.put("Page", paging.getPage());
                object.put("Pages", paging.getPages());
                object.put("hasPreviousPage", paging.hasPreviousPage());
                object.put("hasNextPage", paging.hasNextPage());
                object.put("list", paging.getList());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            if (page.length() != 0 && pageSize.length() != 0) {
                int count = Integer.parseInt(pageSize);
                int one = Integer.parseInt(page);
                int start = (one-1)*count;
                data2 = pagemapper.getPageArtic(count,start);
                for (int i = 0; i < data2.size(); i++) {
                    Text text = new Text();
                    text.setAudit(data2.get(i).getAudit());
                    text.setHeadline(data2.get(i).getHeadline());
                    text.setPublish_date(data2.get(i).getPublish_date());
                    text.setLike_number(data2.get(i).getLike_number());
                    text.setPage(data2.get(i).getPage());
                    list2.add(text);
                }
                PageHelper paging = new PageHelper(total, page, pageSize, list2);
                object = new JSONObject();
                try {
                    object.put("Total", paging.getTotal());
                    object.put("Page", paging.getPage());
                    object.put("Pages", paging.getPages());
                    object.put("hasPreviousPage", paging.hasPreviousPage());
                    object.put("hasNextPage", paging.hasNextPage());
                    object.put("list", paging.getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return Collections.singletonList(object);
    }

}
